<?php

namespace vico\reservarBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use vico\reservarBundle\Entity\Booking;
use vico\reservarBundle\Form\BookingType;

/**
 * Booking controller.
 *
 */
class BookingController extends Controller
{

    /**
     * Lists all Booking entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('rsBundle:Booking')->findAll();

								$deleteForm = array();
								$i = 0;
								foreach( $entities as $entity){
									$deleteForm[$entity->getId()] = $this->createDeleteForm($entity->getId())->createView();	
								}
        return $this->render('rsBundle:Booking:index.html.twig', array(
            'entities' => $entities,
												'title_page'=>'Gestión Reservas',
												'delete_form'=>$deleteForm,
        ));
    }
    /**
     * Lists all Pending Booking entities.
     *
     */
    public function pendingAction()
    {
        $em = $this->getDoctrine()->getManager();

								$status_id = 6;
								$entities = $em->getRepository('rsBundle:Booking')->findByNotStatus($status_id);

								$deleteForm = array();
								$i = 0;
								foreach( $entities as $entity){
									$deleteForm[$entity->getId()] = $this->createDeleteForm($entity->getId())->createView();	
								}

        return $this->render('rsBundle:Booking:index.html.twig', array(
            'entities' => $entities,
												'title_page'=>'Reservas Pendientes',
												'delete_form'=>$deleteForm,
        ));
    }

    /**
     * Creates a new Booking entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new Booking();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
											$this->get('session')->getFlashBag()->add('info',
											   '¡Se ha generado una nueva reserva!'
				        );
            return $this->redirect($this->generateUrl('administracion_booking', array('id' => $entity->getId())));
        }

        return $this->render('rsBundle:Booking:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
    * Creates a form to create a Booking entity.
    *
    * @param Booking $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createCreateForm(Booking $entity)
    {
        $form = $this->createForm(new BookingType(), $entity, array(
            'action' => $this->generateUrl('administracion_booking_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Añadir Reserva','attr'=>array('class'=>'btn btn-xs btn-link')));

        return $form;
    }

    /**
     * Displays a form to create a new Booking entity.
     *
     */
    public function newAction()
    {
        $entity = new Booking();
        $form   = $this->createCreateForm($entity);

        return $this->render('rsBundle:Booking:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a Booking entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('rsBundle:Booking')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Booking entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('rsBundle:Booking:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),        ));
    }

    /**
     * Displays a form to edit an existing Booking entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('rsBundle:Booking')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Booking entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('rsBundle:Booking:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
    * Creates a form to edit a Booking entity.
    *
    * @param Booking $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Booking $entity)
    {
        $form = $this->createForm(new BookingType(), $entity, array(
            'action' => $this->generateUrl('administracion_booking_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

								$form->add('submit', 'submit', array('label' => 'Editar',
																																					'attr'=>array('class'=>'btn btn-xs btn-link')	
								));

        return $form;
    }
    /**
     * Edits an existing Booking entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('rsBundle:Booking')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Booking entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);

	if ($editForm->isValid()) {
		$em->flush();
		$this->get('session')->getFlashBag()->add('info',
		   '¡Los datos de la reserva han sido actualizados!'
	        );
		$STATUS_ID_PAGADA = 3;
		if( $entity->getStatus() == "pagada" ){
		$vico = 'obraservico@vgsgestion.com';
		$pedro = 'pedrocantoria@gmail.com';
		$message = \Swift_Message::newInstance()
			->setSubject('Asignacion')
			->setFrom('info@grupovico.com')
			->setTo($pedro)
			->setBody(
				$this->renderView(
				'rsBundle:Default:email.html.twig',
				array('booking' => $entity)	
				));
			$this->get('mailer')->send($message);
		}
            return $this->redirect($this->generateUrl('administracion_booking_edit', array('id' => $id)));
        }

        return $this->render('rsBundle:Booking:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    /**
     * Deletes a Booking entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('rsBundle:Booking')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Booking entity.');
            }

            $em->remove($entity);
            $em->flush();
											$this->get('session')->getFlashBag()->add('info',
											   '¡La reserva ha sido eliminada con éxito!'
				        );

        }

        return $this->redirect($this->generateUrl('administracion_booking'));
    }

    /**
     * Creates a form to delete a Booking entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('administracion_booking_delete', array('id' => $id)))
            ->setMethod('DELETE')
												->add('submit', 'submit', array('label' => 'Borrar',
																																												'attr' => array(
																																														'class'=>'btn btn-xs btn-link'
																																												),
																																												))
            ->getForm()
        ;
    }
}
